package com.example.administrator.rxjavadiydemo.util;



import com.example.library.utils.netutils.IDataCallBack;
import com.example.library.utils.netutils.ResponseBean;


import java.net.SocketTimeoutException;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lw on 2017/11/20.
 */

public class RxJavaNetUtils {

    private RxJavaNetUtils(){}
    public static RxJavaNetUtils getInstance(){
        return LazyHolder.rxJavaNetUtils;
    }

    private static final class LazyHolder{
        private static RxJavaNetUtils rxJavaNetUtils = new RxJavaNetUtils();
    }


    private Subscription mSubscription;
    public  Subscription deploy(Observable observable, final IDataCallBack callback){
        return  observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBean>() {
                    @Override
                    public void onCompleted() {
                        callback.after();
                    }
                    @Override
                    public void onError(Throwable e) {
                        if(callback!=null){
                            if(e instanceof SocketTimeoutException)
                                callback.onFailed("服务器异常");
                            else
                                callback.onFailed(e.getMessage());
                        }
                    }
                    @Override
                    public void onStart() {
                        super.onStart();
                        callback.before();
                    }

                    @Override
                    public void onNext(ResponseBean t) {
                        if(callback!=null) {

                            callback.onCompleted(t);
                        }
                    }
                });
    }

    public  Subscription deployList(Observable observable, final IDataCallBack callback){
       return observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<ResponseBean>>() {
                    @Override
                    public void onCompleted() {
                        callback.after();
                    }
                    @Override
                    public void onError(Throwable e) {
                        if(callback!=null){
                            if(e instanceof SocketTimeoutException)
                                callback.onFailed("服务器异常");
                            else
                                callback.onFailed(e.getMessage());
                        }
                    }
                    @Override
                    public void onStart() {
                        super.onStart();
                        callback.before();
                    }
                    @Override
                    public void onNext(List<ResponseBean> t) {
                        if(callback!=null) {
                            callback.onCompletedList(t);
                        }
                    }
                });
    }
}
